<template>
  <div id="UserManagePage">
    <a-form :model="searchParams" layout="inline" @finish="doSearch" style="padding-bottom: 20px">
      <a-form-item label="ISBN编号">
        <a-input
          v-model:value="searchParams.record_bookisbn"
          placeholder="请输入ISBN编号"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="借阅人">
        <a-input
          v-model:value="searchParams.record_borrower"
          placeholder="请输入借阅人"
          allow-clear
        />
      </a-form-item>
      <a-button type="primary" htmlType="submit">搜索</a-button>
    </a-form>
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
      :loading="loading"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'record_borrowtime'">
          <span>{{ dayjs(record?.record_borrowtime).format('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
        <template v-else-if="column.dataIndex === 'record_remandtime'">
          <span>{{ dayjs(record?.record_remandtime).format('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
      </template>
    </a-table>
  </div>
</template>

<script lang="ts" setup>
import { deleteRecordUsingPost, listRecordVoByPageUsingPost } from '@/api/recordController'
import dayjs from 'dayjs'
import { computed, onMounted, reactive, ref } from 'vue'
//列名称
const columns = [
  {
    title: 'id',
    dataIndex: 'record_id',
    width: 80,
  },
  {
    title: '图书名称',
    dataIndex: 'record_bookname',
  },
  {
    title: 'ISBN编号',
    dataIndex: 'record_bookisbn',
  },
  {
    title: '图书借阅人',
    dataIndex: 'record_borrower',
  },
  {
    title: '图书借阅时间',
    dataIndex: 'record_borrowtime',
  },
  {
    title: '图书归还时间',
    dataIndex: 'record_remandtime',
  },
  {
    title: '操作',
    key: 'action',
  },
]

const dataList = ref([])
const total = ref()

//搜索条件
const searchParams = reactive<API.RecordQueryRequest>({
  current: 1,
  pageSize: 10,
})

//分页参数
const pagination = computed(() => {
  return {
    current: searchParams.current,
    pageSize: searchParams.pageSize,
    total: total.value,
    showSizeChanger: true,
    showQuickJumper: true,
    showTotal: (total: number) => `共 ${total} 条`,
  }
})

//获取借阅列表
const getUserList = async () => {
  const res = await listRecordVoByPageUsingPost(searchParams)
  console.log('获取借阅列表', res.data.data?.records)
  if (res.data.data && res.data.code === 0) {
    dataList.value = res.data.data.records
    total.value = res.data.data.total
  }
}

//切换表格页数
const doTableChange = (page: any) => {
  searchParams.current = page.current
  searchParams.pageSize = page.pageSize
  getUserList()
}
//删除用户
const doDelete = async (id: number) => {
  const res = await deleteRecordUsingPost({ id })
  console.log('删除用户', res)
  if (res.data.code === 0 && res.data.data) {
    getUserList()
  }
}
//搜索
const doSearch = () => {
  searchParams.current = 1
  console.log('搜索', searchParams)
  getUserList()
}
onMounted(() => {
  getUserList()
})
</script>

<style lang="scss" scoped></style>
